package com.suray.wcs.service.websocket;

import com.suray.basic.wcs.path.core.Node;
import com.suray.basic.wcs.plc.core.Plc;
import com.suray.basic.wcs.plc.pojo.oper.lifter.LifterOper;
import com.suray.wcs.service.pojo.HandHoist;
import com.suray.basic.wcs.plc.entity.PlcDeviceDB;
import com.suray.wcs.service.system.service.impl.HoisterOperServiceImp;
import com.suray.basic.wcs.plc.service.impl.PlcDeviceDBServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:定时向前端推送信息
 * @author: rongbj
 * @date:2019年9月9日 下午5:53:54
 */
@Component
public class DeviceInfoPush {
    /**
     * 日志记录
     */
    private static Logger log = LoggerFactory.getLogger(DeviceInfoPush.class);
    /**
     * 提升机信息查询
     */
    @Autowired
    private PlcDeviceDBServiceImpl plcdevicedbServiceImpl;

    @Autowired
    private HoisterOperServiceImp hoisterOperServiceImp;

    public DeviceInfoPush() {
        super();
    }

    /**
     * 向前端推送小车仓库监控界面小车列表数据
     * 间隔1秒执行一次
     */
    @Scheduled(cron = "0/1 * * * * ?")
    public void rgvListPush() {
        try {
            WebSocketServer.sendRgvListInfo();
        } catch (Exception e) {
            log.error("小车信息推送遇到 错误！");
            e.printStackTrace();
        }
    }

    @Scheduled(cron = "0/1 * * * * ?")
    public void lifterListPush() {
        try {
            WebSocketServer.sendLifterListInfo();
        } catch (Exception e) {
            log.error("提升机信息推送遇到 错误！");
            e.printStackTrace();
        }
    }

    /**
     * 向前端推送当前层小车和工位数据
     */
    @Scheduled(cron = "0/1 * * * * ?")
    public void currentFloorPush() {
        try {
            WebSocketServer.sendCurrLayerInfo();
        } catch (Exception e) {
            log.error("当前层小车信息和工位信息推送遇到 错误！");
            e.printStackTrace();
        }
    }

    /**
     * 当前节点对应的设备的信息
     */
    @Scheduled(cron = "0/1 * * * * ?")
    public void currentNodePush() {
        try {
            WebSocketServer.sendCurrCoordInfo();
        } catch (Exception e) {
            log.error("当前坐标信息推送遇到 错误！");
            e.printStackTrace();
        }
    }

    /**
     * @return List<HandHoist>
     * @Description:查询处理并返回提升机信息
     */
    public List<HandHoist> getHandHoists() {
        List<HandHoist> hoists = new ArrayList<>();
        List<PlcDeviceDB> plcDeviceDBS = plcdevicedbServiceImpl.queryLiftList();
        LifterOper lifterOper;
        if (plcDeviceDBS != null && plcDeviceDBS.size() > 0) {
            for (PlcDeviceDB plcdevicedb : plcDeviceDBS) {
                HandHoist handHoist = new HandHoist();
                handHoist.setDeviceLocationNode(Node.parseGrid(plcdevicedb.getDeviceLocation()));
                handHoist.setHoistNo(Integer.toString(plcdevicedb.getDeviceId()));
                handHoist.setHoistName(plcdevicedb.getDeviceName());
                lifterOper = (LifterOper) Plc.deviceBaseNameMap.get(plcdevicedbServiceImpl.getById(plcdevicedb.getDeviceId()).getDeviceName());
                if (lifterOper == null) {
                    handHoist.setReadyState("离线");
                } else {
                    handHoist = hoisterOperServiceImp.getHoistInfo(lifterOper);
                    handHoist.setDeviceLocation(
                            //前端需要格式为x,y,z的坐标
                            plcdevicedb.getDeviceLocation().substring(plcdevicedb.getDeviceLocation().indexOf("(") + 1,
                                    plcdevicedb.getDeviceLocation().indexOf(")")));
                    handHoist.setHoistName(plcdevicedb.getDeviceName());
                    handHoist.setHoistNo(Integer.toString(plcdevicedb.getDeviceId()));
                }
                if (handHoist != null) {
                    hoists.add(handHoist);
                }
            }
        } else {
            hoists = null;
        }
        return hoists;
    }

}
